package com.simon.jkxj

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.simon.jkxj.databinding.ActivityRegisterBinding
import com.simon.jkxj.model.AppDatabase
import com.simon.jkxj.model.User
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class RegisterActivity : AppCompatActivity() {
    private lateinit var binding: ActivityRegisterBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupClickListeners()
    }

    private fun setupClickListeners() {
        binding.btnRegister.setOnClickListener {
            val username = binding.etUsername.text.toString()
            val phone = binding.etPhone.text.toString()
            val password = binding.etPassword.text.toString()
            val confirmPassword = binding.etConfirmPassword.text.toString()

            if (validateInput(username, phone, password, confirmPassword)) {
                lifecycleScope.launch {
                    val userDao = AppDatabase.getDatabase(this@RegisterActivity).userDao()
                    val exist = withContext(Dispatchers.IO) { userDao.getUserByPhone(phone) }
                    if (exist != null) {
                        withContext(Dispatchers.Main) {
                            binding.tilPhone.error = "手机号已注册"
                        }
                    } else {
                        val user = User(username = username, phone = phone, password = password)
                        withContext(Dispatchers.IO) { userDao.insertUser(user) }
                        withContext(Dispatchers.Main) {
                            Toast.makeText(this@RegisterActivity, "注册成功", Toast.LENGTH_SHORT).show()
                            finish()
                        }
                    }
                }
            }
        }

        binding.tvLogin.setOnClickListener {
            finish()
        }
    }

    private fun validateInput(
        username: String,
        phone: String,
        password: String,
        confirmPassword: String
    ): Boolean {
        if (username.isEmpty()) {
            binding.tilUsername.error = "请输入用户名"
            return false
        }
        if (phone.isEmpty()) {
            binding.tilPhone.error = "请输入手机号"
            return false
        }
        if (phone.length != 11) {
            binding.tilPhone.error = "请输入正确的手机号"
            return false
        }
        if (password.isEmpty()) {
            binding.tilPassword.error = "请输入密码"
            return false
        }
        if (password.length < 6) {
            binding.tilPassword.error = "密码长度不能少于6位"
            return false
        }
        if (confirmPassword.isEmpty()) {
            binding.tilConfirmPassword.error = "请确认密码"
            return false
        }
        if (password != confirmPassword) {
            binding.tilConfirmPassword.error = "两次输入的密码不一致"
            return false
        }
        return true
    }
} 